// packageE/appointment/store/porjectEdit/porjectEdit.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    columns: [],
    classArray: [],
    form: {
      title: "",
      call_out: "0",
      service_time: 30,
      service_time_inteval: 0,
      ahead_time: 15,
      content: "", //简介
      description: "", //描述
      category_id: "",
      thumb: "",
      banner_imgs: ""
    },
    isclick: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getClass();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.options.id) {
      // 编辑
      this.getProject();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  async getProject() {
    wx.showLoading({
      title: '加载中',
    })
    let {
      data
    } = await app._getNetWork({
      url: app.getNetAddresss("plugin.appointment.frontend.project.store-project-detail"),
      data: {
        id: this.options.id
      },
    });
    wx.hideLoading();
    if (data.result == 1) {
      this.data.form = {
        ...data.data
      }
      this.data.form.call_out = String(this.data.form.call_out);
      this.setData({
        form: this.data.form
      })
    } else {
      app.tips(data.msg)
    }
    console.log(data)

  },
  inputBlur(e) {
    console.log(e)
    let {
      key
    } = e.currentTarget.dataset;
    let {
      value
    } = e.detail;
    console.log(key, value)
    this.data.form[key] = value;
    this.setData({
      form: this.data.form
    })
    console.log(this.data.form)
  },
  radioChange(e) {
    this.setData({
      'form.call_out': e.detail
    })
  },
  onConfirm(e) {
    let {
      index
    } = e.detail;
    this.setData({
      'form.category_id': this.data.classArray[index].id,
      show: false
    })
  },
  onCancel(e) {
    this.setData({
      show: false
    })
  },
  showOpen(e) {
    this.setData({
      show: true
    })
  },
  async getClass() {
    let {
      data
    } = await app._getNetWork({
      url: app.getNetAddresss("plugin.appointment.frontend.project-category.get-list")
    });
    if (data.result == 1) {
      let array = [];
      if (data.data.length > 0) {
        for (let i = 0; i < data.data.length; i++) {
          array.push(data.data[i].name);
        }
      }
      console.log(array,'ddddddd')
      this.setData({
        classArray: data.data,
        columns: array
      })
    } else {
      app.tips(data.msg)
    }
  },
  async submit() {
    if (this.data.isclick) return;
    this.data.isclick = true;
    let json = {
      form: this.data.form
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.appointment.frontend.project.store-project-save"),
      data: json
    });
    if (!data.result) {
      app.tips(data.msg);
      return
    }
    setTimeout(() => {
      this.data.isclick = false;
      wx.navigateBack({
        delta: 1
      })
    }, 1000);
    app.tips(data.msg);

  },
  onRead1(event) {
    const {
      file
    } = event.detail;
    let that = this;
    let urlStr = app.getNetAddresss("upload.uploadPic");
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      success: (resdata) => {
        let {
          result,
          msg,
          data
        } = JSON.parse(resdata.data);
        if (result) {
          console.log(data.img_url);
          this.setData({
            'form.banner_imgs': data.img_url
          })
        };
        const {
          fileList = []
        } = that.data;

      },
      fail(err) {
        let fileList = [];
      }
    });
  },
  // 上传图片
  multipleMethod(event) {
    const {
      file
    } = event.detail;
    let that = this;
    let urlStr = app.getNetAddresss("upload.uploadPic");
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      success: (resdata) => {
        let {
          result,
          msg,
          data
        } = JSON.parse(resdata.data);
        if (result) {
          console.log(data.img_url);
          this.setData({
            'form.thumb': data.img_url
          })
        };
        const {
          fileList = []
        } = that.data;

      },
      fail(err) {
        let fileList = [];
      }
    });
  },
})